//
//  AuditedAlertView.swift
//  ZYA
//
//  Created by hudan on 2025/6/25.
//  Copyright © 2025 zwb. All rights reserved.
//

import UIKit

class AuditedAlertView: UIView {
    
    private lazy var contentView: UIView = {
        let view = UIView().extBgColor(color: .white)
            .extRadius(radius: 5).extMaskBounds(mask: true)
        return view
    }()
    
    private lazy var textView: UITextView_P = {
        let textView = UITextView_P().extBgColor(color: UIColor(kshex: "#f2f2f2"))
            .extRadius(radius: 5).extMaskBounds(mask: true)

        textView.font = .systemFont(ofSize: 16, weight: .medium)
        textView.placeHolder = "请输入消审备注"
        textView.plaAlignment = .left
        
        return textView
    }()
    
    private var actionBlock: ((String) -> Void)?
    
    
    override init(frame: CGRect) {
        super.init(frame: UIScreen.main.bounds)
        
        initUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func initUI() {
        extBgColor(color: .init(white: 0, alpha: 0.6))
                
        let margin: CGFloat = 30
        let size = CGSizeMake(KS_SCREEN_WIDTH - 2*margin, 200)
        contentView.frame = CGRect(origin: CGPoint(x: (KS_SCREEN_WIDTH - size.width)/2, y: (KS_SCREEN_HEIGHT - size.height)/2 - 100), size: size)
        addSubview(contentView)
        
//        let titleLab = UILabel().extFont(font: .boldSystemFont(ofSize: 18))
//            .extTextColor(color: .black)
//            .extTextAligment(aligment: .center)
//        contentView.addSubview(titleLab)
//        titleLab.snp.makeConstraints { make in
//            make.left.equalToSuperview().offset(10)
//            make.right.equalToSuperview().offset(-10)
//            make.top.equalToSuperview().offset(10)
//            make.height.equalTo(20)
//        }
        
        
        contentView.addSubview(textView)
        textView.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(20)
            make.right.equalToSuperview().offset(-20)
            make.top.equalToSuperview().offset(20)
            make.height.equalTo(120)
        }
        
        let sureBtn = UIButton().easy("确定", .white, .systemFont(ofSize: 18, weight: .medium), nil, self, #selector(sureBtnClick))
            .extBgColor(color: Theme.skyBlue())
            .extRadius(radius: 5).extMaskBounds(mask: true)
        contentView.addSubview(sureBtn)
        sureBtn.snp.makeConstraints { make in
            make.height.equalTo(40)
            make.left.equalToSuperview().offset(20)
            make.right.equalTo(contentView.snp.centerX).offset(-20)
            make.bottom.equalToSuperview().offset(-10)
        }
        
        let cancelBtn = UIButton().easy("取消", .lightGray, .systemFont(ofSize: 18, weight: .medium), nil, self, #selector(dismiss))
            .extBgColor(color: UIColor(kshex: "#f2f2f2"))
            .extRadius(radius: 5).extMaskBounds(mask: true)
            .extBorderW(width: 1).extBorderColor(color: UIColor(kshex: "#cccccc"))
        contentView.addSubview(cancelBtn)
        cancelBtn.snp.makeConstraints { make in
            make.left.equalTo(contentView.snp.centerX).offset(20)
            make.right.equalToSuperview().offset(-20)
            make.height.bottom.equalTo(sureBtn)
        }
    }
    
    
    @objc private func sureBtnClick() {
        
        if textView.text?.count ?? 0 > 0 {
            actionBlock?(textView.text!)
            
            dismiss()
        } else {
            KSToast("备注信息不能为空")
        }
    }
    
    func show(action: @escaping ((String) -> Void)) {
        actionBlock = action
        
        if let keyWindow = UIApplication.shared.windows.filter({ $0.isKeyWindow }).first {
            
            keyWindow.addSubview(self)
            
            contentView.alpha = 0.3
            contentView.transform = CGAffineTransform(scaleX: 0.8, y: 0.8)
            UIView.animate(withDuration: 0.3) {
                self.contentView.alpha = 1
                self.contentView.transform = CGAffineTransformIdentity
            } completion: { _ in
                DispatchQueue.main.async {
                    self.textView.becomeFirstResponder()
                }
            }
        }
    }
    
    @objc func dismiss() {
        
        textView.resignFirstResponder()
        
        UIView.animate(withDuration: 0.3) {
            self.contentView.alpha = 0.05
            self.contentView.transform = CGAffineTransform(scaleX: 0.1, y: 0.1)
            self.backgroundColor = .init(white: 0, alpha: 0.1)
        } completion: { _ in
            self.removeFromSuperview()
        }
    }
}
